package com.api.gbhg.luoao.utils;
import com.api.gbhg.lbj.dao.GeneralProjectDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vba.word.Lines;
import weaver.conn.RecordSet;

import java.util.HashMap;
import java.util.Map;

public class BudgetLinesFunction {

    private final static Logger logger = LoggerFactory.getLogger("gbhg");

    /**
     * 根据表名、流程id，查询数据
     *
     */
    public static boolean  sumQuery(String lcjlId,String fromtable){
        PropertyManagementCostDao p=new PropertyManagementCostDao();
        String price1 = "dt2.hjwy";
        String price2 = "hjwy";
        if(fromtable.equals("uf_txrygzjtbtbb")){
            price1 = "dt2.hj";
            price2 = "hj";
        }
        //查询版本表中的数据
        RecordSet rs=new RecordSet();
        String sql = "select \n" +
                "ver.xmmin,ver.sbdw,ver.lcbj,ver.lcjlsqjlid,ver.ysnd,tab.id,dt2.bmyszcjjfl,dt2.dyzfjjfl," +
                "ver.jbr,ver.gkdw,ver.sydw,"+price1+"\n" +
                "from \n" +
                "uf_jingfei_version ver,\n" +
                ""+fromtable+" tab,\n" +
                ""+fromtable+"_dt2 dt2\n" +
                "where ver.lcjlsqjlid = tab.requestId and tab.id = dt2.mainid and ver.lcjlsqjlid ="+lcjlId;
        rs.executeQuery(sql);

        //删除原先的数据
        boolean result = false;
        RecordSet recordSet = GeneralProjectDao.deleteWorkflowByid(lcjlId);
        if (recordSet.next()){
            result = true;
        }
        logger.info("删除原先的数据"+result);

        //开始插入数据进经费额度表中
        Map<String,String> map = new HashMap<>();
        boolean b = false;
        while (rs.next()){
            map.put("requestId",rs.getStringNoTrim("lcjlsqjlid"));
            map.put("lcbj",rs.getStringNoTrim("lcbj"));
            map.put("lcjlid",rs.getStringNoTrim("lcjlsqjlid"));
            map.put("nd",rs.getStringNoTrim("ysnd"));
            String hjwy = rs.getStringNoTrim(price2);
            if(hjwy.isEmpty()){
                hjwy="0.00";
            }
            map.put("ze",hjwy);
            map.put("bmzcjjfl",rs.getStringNoTrim("bmyszcjjfl"));
            map.put("zfjjfl",rs.getStringNoTrim("dyzfjjfl"));
            map.put("syed",hjwy);
            map.put("dj","0.00");
            map.put("sbdw",rs.getStringNoTrim("sbdw"));
            map.put("xmmc",rs.getStringNoTrim("xmmin"));
            //添加归口单位、使用单位
            map.put("gkdw",rs.getStringNoTrim("gkdw"));
            map.put("sydw",rs.getStringNoTrim("sydw"));
            map.put("modedatamodifier",rs.getStringNoTrim("jbr"));
            map.put("modedatamodifydatetime", DateUtil.getNowDate());
            b = p.insertBudgeLines(map);
            if (b == false){
                break;
            }
        }

        return b;
    }

}
